<?php
class conexion extends PDO
{
	private  $server="localhost";
    private  $user="nehuen";
    private  $pass="ZXCasd123";
    private  $database ="mtg";
    private  $queryS = "";
    private  $queryT = "";
    private  $queryD = "";
	public $connObj;
	
	public  function __construct() {
	}
	
	public function coneccionBd(){
		try {
		$this->connObj  = new PDO('mysql:host='.$this->server.';dbname='.$this->database, $this->user,$this->pass);
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
	public function closeConn(){
		$this->connObj = null;
	}
	public function queryExec($queryS){
		try { 
			$this->coneccionBd();
			$this->connObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$pre = $this->connObj->prepare($queryS);
			$pre->execute();
			$resultArray = $this->returnQueryFetch($pre);
			//$resultArray['lastId'] = $this->lastInsertId();
			$this->closeConn();
		} catch (Exception $e) {
			$this->connObj->rollBack();
			echo "Failed: " . $e->getMessage();
			$this->closeConn();
		}
			return $resultArray;
	}
	public function insertData($tabla,$tablas){
		$this->queryS = "INSERT INTO ".$tabla." (";
		$this->eachQuery($tablas);
		$this->queryS .= $this->queryT.") VALUES (".$this->queryD.");";
		return $this->queryS;
	}
	public function deleteData($tabal, $tablas){
		$this->queryS .= "DELETE FROM ".$tabal." WHERE ";
		$this->eachQuery($tablas);
		$this->queryS .= $this->queryT." = ".$this->queryD.";";
		return $this->queryS;
	}
	public function updateData($tabal,$tablasU,$idUpdate,$columU){
		$this->queryS .= "UPDATE ".$tabal." SET ";
		foreach($tablasU as $columna => $datos){
			$this->queryS .= $columna." = ".$datos.",";
		}
		$this->queryS = trim($this->queryS,",") ." WHERE ".$columU." = ".$idUpdate.";"; 
		return $this->queryS;
	}
	
	public function eachQuery($tablas){
		foreach($tablas as $columna => $datos){
			$this->queryT .= $columna.",";
			$this->queryD .= $datos.",";
		}
		$this->queryT = trim($this->queryT, ',');
		$this->queryD = trim($this->queryD, ',');
	}
	
	private function returnQueryFetch($result){
		$row = $result->fetchALL(PDO::FETCH_ASSOC);
		return $row;
	}
	
	
}
?>